package xin.printk.android.module.splash;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

import java.util.Locale;

import xin.printk.android.BasicFragment;
import xin.printk.android.MainActivity;
import xin.printk.android.R;
import xin.printk.android.module.guide.GuideActivity;
import xin.printk.android.utils.AppConstants;
import xin.printk.android.utils.PrefUtils;

public class SplashFragment extends BasicFragment implements SplashContract.View {

    private static final long DELAY_TIME = 3000;

    private TextView mCountdownText;

    private CountDownTimer mCountDownTimer = new CountDownTimer(DELAY_TIME + 100, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mCountdownText.setText(String.format(Locale.getDefault(), "剩余：%d秒", millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            if (PrefUtils.getBoolean(AppConstants.FIRST_START_UP, true)) {
                startActivity(new Intent(getActivity(), GuideActivity.class));
            } else {
                startActivity(new Intent(getActivity(), MainActivity.class));
            }
            getActivity().finish();
            mCountDownTimer = null;
        }
    };

    @Override
    protected int bindLayoutResId() {
        return R.layout.fragment_splash;
    }

    @Override
    protected void onInit() {
        mCountdownText = findView(R.id.tv_countdown);
    }

    @Override
    protected void onExecute(Bundle savedInstanceState) {
        mCountDownTimer.start();
    }

    @Override
    public CountDownTimer getCountDownTimer() {
        return mCountDownTimer;
    }

}
